package com.tanhua.manage.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.manage.vo.CommentVVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentService {

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private CommentApi commentApi;

    public ResponseEntity<Object> queryByPage(Integer page, Integer pagesize, String publishId) {
        PageResult pageResult = commentApi.queryByPID(page, pagesize, publishId);
        List<Comment> items = (List<Comment>) pageResult.getItems();

        ArrayList<CommentVVo> commentVos = new ArrayList<>();

        if (items != null) {
            for (Comment item : items) {
                CommentVVo vo = new CommentVVo();
                BeanUtils.copyProperties(item, vo);

                Long userId = item.getUserId();
                UserInfo userInfo = userInfoApi.findById(userId);
                if (userInfo != null) {
                    BeanUtils.copyProperties(userInfo, vo);
                }
                vo.setId(item.getId().toString());
                vo.setCreateDate(item.getCreated());
                commentVos.add(vo);
            }
        }
        pageResult.setItems(commentVos);
        return ResponseEntity.ok(pageResult);
    }

}
